package com.house.controller;

import cn.hutool.core.bean.BeanUtil;
import com.house.dto.HouseDTO;
import com.house.entity.House;
import com.house.entity.Img;
import com.house.service.HouseService;
import com.house.utils.Code;
import com.house.utils.FileTools;
import com.house.utils.Result;
import com.house.vo.HouseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: GGP
 * @description: 房源控制层
 * @time: 2023-04-12 10:33
 **/
@Api("房源")
@RestController
@RequestMapping("/house")
@Validated
public class HouseController {

	@Resource
	private HouseService houseService;

	/**
	 * 上传单个文件
	 * @param file
	 * @return
	 */
	@ApiOperation(value = "upload",notes = "上传单个文件")
	@PostMapping("/upload")
	public Result upload(@ApiParam(name = "file",value = "单个文件",required = true)@RequestParam("file") MultipartFile file, HttpServletRequest request) {
		if (!FileTools.hasFile(file)) {
			return new Result(Code.SAVE_ERR,"请上传图片");
		}
		if (!FileTools.checkImg(file)){
			return new Result(Code.SAVE_ERR,"文件类型错误");
		}
		String path = FileTools.uploadFile(file,request);
		if (path==null){
			return new Result(Code.SYSTEM_ERR,"系统异常，请联系管理员");
		}
		return new Result(Code.SAVE_OK,path,"上传成功");
	}

	/**
	 * 上传多个文件
	 * @param files
	 * @return
	 */
	@ApiOperation(value = "multipleUpload",notes = "上传多个文件")
	@PostMapping("/multipleUpload")
	public Result multipleUpload(@ApiParam(name = "files",value = "多个文件",required = true) @RequestParam("files") List<MultipartFile> files,HttpServletRequest request) {
		if (files==null||files.isEmpty()) {
			return new Result(Code.SAVE_ERR,"请上传图片");
		}
		if (!FileTools.checkImgList(files)){
			return new Result(Code.SAVE_ERR,"文件类型错误");
		}
		List<String> pathList = FileTools.uploadMoreFile(files,request);
		if (pathList==null){
			return new Result(Code.SYSTEM_ERR,"系统异常，请联系管理员");
		}
		return new Result(Code.SAVE_OK,pathList,"上传成功");
	}

	/**
	 * 添加房源
	 * @return
	 */
	@ApiOperation(value = "save",notes = "添加房源")
	@PostMapping("/save")
	public Result saveHouse(@ApiParam(name = "houseDTO",value = "房源对象",required = true) @RequestBody @Valid HouseDTO houseDTO){
		House saveHouse = houseService.saveHouse(houseDTO);
		if (saveHouse==null){
			return new Result(Code.SAVE_ERR,"添加失败");
		}
		return new Result(Code.SAVE_OK,"添加成功");
	}

	/**
	 * 条件筛选查询
	 * @param houseDTO
	 * @return
	 */
	@ApiOperation(value = "find",notes = "条件筛选查询")
	@PostMapping("/find")
	public Result findHouse(@ApiParam(name = "houseDTO",value = "房源对象",required = true) @RequestBody HouseDTO houseDTO){
		Map<String,Object> map=new HashMap<>(3);
		Page<Map<String, Object>> allHouse = houseService.findAllHouse(houseDTO);
		List<Map<String, Object>> content = allHouse.getContent();
		List<HouseVO> housesVO = BeanUtil.copyToList(content, HouseVO.class);
		housesVO.forEach(houseVO -> {
			List<String> imgPaths=new ArrayList<>();
			List<Img> imgs = houseService.findImgs(houseVO.getId());
			imgs.forEach(img -> {
				imgPaths.add(img.getUrl());
			});
			houseVO.setImgs(imgPaths);
		});
		map.put("houses",housesVO);
		map.put("totalPages",allHouse.getTotalPages());
		map.put("totalElements",allHouse.getTotalElements());
		return new Result(Code.GET_OK,map,"查询成功");
	}

	/**
	 * 通过id查询房源-详情
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "/findById/{id}",notes = "房源详情")
	@GetMapping("/findById/{id}")
	public Result getById(@ApiParam(name = "id",value = "房源id",required = true) @PathVariable Integer id){
		HouseVO houseVO = houseService.findHouseById(id);
		return new Result(Code.GET_OK,houseVO,"查询成功");
	}

}
